#!/perl/bin/perl
#
###############################################################
#
# Auto-Comment for ActionScript 2.0
#
# as2comment.pl
#
# v0.9 - 12-03-2006
#
# By J.L.Milkins
# comments / donations to: jason@mentalaxis.com
#
# Licenced under LGPL 
#
###############################################################

use XML::Parser;
use Getopt::Std;

use ParamParser;
use AutoJavaDoc;
use AutoNaturalDoc;

package main;

$isClass = 1;

# Gather parameters from $ARGV
( $inFile, $outFile, $inType ) = @ARGV;



ParamParser->getSettings ("settings.xml");

if ( $inType =~ m/n/ ) {
	$docType = 0; #NaturalDocs
} else {
	$docType = 1; #JavaDoc
}

if ( $#ARGV == -1 ) {
	helpMessage();
}
if ( $#ARGV == 0 ) {
	helpMessage();
	die;
}
 
# Init loop vars...
$braceCount        = 0;
$blockCommentCount = 0;

# Run main loop using the first parameter passed at the command line...
$document = docFile($inFile);

# output the $documented version to the outFile $ARGV[1])
outputDoc( $outFile, $document );

###############################################################
# sub: outputDoc
sub outputDoc {
	my ( $fileName, $outputText ) = @_;
	open( OUTFILE, "> $fileName" )
	  or die "Couldn't open $fileName for output: $!\n";
	select OUTFILE;
	print $outputText;
	close OUTFILE;
}
###############################################################
# sub: docFile
sub docFile {
	my ($fileName) = @_;

	#init Output
	$output = "";

	#init prevDone;
	$previous = 0;

	# open file...
	open SOURCE, "< $fileName"
	  or die "Couldn't open $fileName for reading: $!\n";

	# Read the file into an array of lines...
	@lines = <SOURCE>;
	close SOURCE;
	for my $line (@lines) {
		if ( $previous != 1 ) {
			if ( $line =~ m!\*/! ) {
				$previous = 1;
			} else {
				$previous = 0;
			}

			# Update block comment count
			$openBlockComment = $line =~ m!/\*!;
			$blockCommentCount += $openBlockComment;
			$closeBlockComment = $line =~ m!\*/!;
			$blockCommentCount -= $closeBlockComment;

			# If we are not inside a block comment check/document the line...
			if ( $blockCommentCount == 0 ) {

				# Update brace count
				$openBrace = $line =~ m/{/;
				$braceCount += $openBrace;
				$closeBrace = $line =~ m/}/;
				$braceCount -= $closeBrace;

				# output the documentation for the current line...
				$doc = docline( $line, $braceCount, $docType);

				# send documentation to output
				$output .= $doc;
			}
		} else {
			$previous = 0;
		}
		$output .= $line;
	}

	#return output
	return $output;
}

###########################################################################
# sub: docline
# Process a line and build documentation for class, interface and members.
sub docline {
	my ( $inLine, $braceCount, $docStyle ) = @_;

	$lineDoc      = "";
	$currentBrace = $inLine =~ m/{/;
	
	if($docStyle == 1) {
		$lineDoc = AutoJavaDoc->checkClass( $inLine, $braceCount, $currentBrace, $inAuthor );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoJavaDoc->checkClassMethod( $inLine, $braceCount, $currentBrace );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoJavaDoc->checkInterface( $inLine, $braceCount, $currentBrace, $inAuthor );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoJavaDoc->checkInterfaceMethod( $inLine, $braceCount, $currentBrace );
		if ( $lineDoc ne "" ) { return $lineDoc; }
	} else {
		$lineDoc = AutoNaturalDoc->checkClass( $inLine, $braceCount, $currentBrace, $inAuthor );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoNaturalDoc->checkClassProperty( $inLine, $braceCount, $currentBrace );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoNaturalDoc->checkClassMethod( $inLine, $braceCount, $currentBrace );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoNaturalDoc->checkInterface( $inLine, $braceCount, $currentBrace, $inAuthor );
		if ( $lineDoc ne "" ) { return $lineDoc; }
		$lineDoc = AutoNaturalDoc->checkInterfaceMethod( $inLine, $braceCount, $currentBrace );
		if ( $lineDoc ne "" ) { return $lineDoc; }	
	}
}

###############################################################
# sub: helpMessage
#
# Display the help message when an invalid number of parameters
# are passed to the script at runtime
#
sub helpMessage {
	
# Console formatters
$divider   = ( '=' x 80 ) . "\n";
	
print <<"EOF";
$divider
Auto-Comment for ActionScript 2.0
(Licenced under LGPL)

as2comment - v0.9 - 12-03-2006 (Perl)

Usage:
[perl] as2comment.pl inputfilename [-opts]

[inputfilename] is an AS2.0 Class filename to process
options

*Recommend you have a backup of the original file.

[type] is either n (naturaldoc) or j (javadoc) default to JavaDoc
[authorname] is the class authorname.

(square brackets [] denote optional parameters)

e.g.
To add JavaDoc comments to a class called Input.as by jamilk. 
(outputting to Input.as)

perl as2comment.pl Input.as Input.as j jamilk

To add NaturalDoc comments to a class called Input.as by jamilk. 
(outputting to Input.as)

perl as2comment.pl Input.as Input.as n jamilk

$divider
written by J.L.Milkins
jason\@mentalaxis.com
http://mentalaxis.com/

$divider
$indent ! WARNING ! USE ENTIRELY AT YOUR OWN RISK !

$indent The author accept no responsibility for any loss
$indent or damage as a result of use of this software

$indent Make sure you have a backups of the class files
$indent that you intend to run AS2Comment on.

EOF
}


